package com.cykj.page05.dto;

/**
 * @BelongsProject: AltronBoot
 * @BelongsPackage: org.wql.dto
 * @Author: 阿隆
 * @CreateTime: 2024-10-18 17:13
 * @Description: 统一格式返回前端数据传输对象
 * @Version: 1.0
 */

public class ResponseDTO {
    //code 字段表示响应状态码，一般用整数值表示不同的状态。
    private Integer code;
    //msg 字段表示响应消息，用字符串表示具体的描述信息。
    private String msg;
    //data 字段用于存储具体的数据对象，可以根据需要指定其类型。
    private Object data;

    //成功状态码
    private static final Integer SUCCESS = 1;
    //失败状态码
    private static final Integer ERROR = -1;

    public ResponseDTO(Integer code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    @Override
    public String toString() {
        return "ResponseDTO{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }

    /**
     * @description: 成功调用的DTO (传输数据)
     * @Param o:Object
     * @return: com.wql.dto.ResponseDTO
     */
    public static ResponseDTO success(Object o) {
        return new ResponseDTO(SUCCESS, "success", o);
    }

    /**
     * @description: 失败调用的DTO
     * @Param o:Object
     * @return: com.wql.dto.ResponseDTO
     */
    public static ResponseDTO fail() {
        return new ResponseDTO(ERROR, "error", null);
    }

    /**
     * @Description: 失败调用传输消息
     * @Param: [msg]
     * @return: org.wql.dto.ResponseDTO
     */
    public static ResponseDTO fail(String msg) {
        return new ResponseDTO(ERROR, msg, null);
    }


    /**
     * @Description: 返回值是影响的行数
     * @Param: [res]
     * @return: org.wql.dto.ResponseDTO
     */
    public static ResponseDTO effectResult(Integer res) {

        return res > 0 ? success(null) : fail(null);
    }


    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }
}
